#include "translation.h"
#include <QCoreApplication>
#include <QDirIterator>
#include <QDebug>
Translation::Translation(QObject *parent) : QObject(parent)
{
    m_translatorEnglish = new QTranslator(this);
}

void Translation::installLanguage(const QString &language, QTranslator *translator)
{
    QDirIterator itor(":/translations/" + language , QStringList("*.qm"), QDir::Files, QDirIterator::Subdirectories);


    while (itor.hasNext())
    {
        itor.next();
        qDebug() << "lang:" << itor.filePath();
        bool r = translator->load(itor.filePath());
          qDebug() << "load language result: " << r;
    }

    bool ok =  qApp->installTranslator(translator);

    qDebug() << "install language result: " << ok;


}

void Translation::selectLanguage(int index)
{
    switch(index) {
    case 0:
        this->installLanguage("en", m_translatorEnglish);
        break;
    case 1:
        qApp->removeTranslator(m_translatorEnglish);
        break;
    default:break;
    }

    emit languageChanged();
}

QStringList Translation::supportedLanguage()
{
    QStringList list;
    list <<  tr("英文");
    list <<  tr("中文");
    return list;
}


